<?php
/**
 *  文章分类模型类
 */
namespace app\admin\model\content;

use core\basic\Model;

class ContentCateModel extends Model
{

    // 存储分类及子编码
    protected $scodes = array();

    // 获取文章分类列表
    public function getList()
    {
        $field = array('*');
        $result = parent::table('ay_content_cate')->field($field)
            ->where("acode='" . session('acode') . "'")
            ->order('pid,sorting,id')
            ->select();
        $tree = get_tree($result, 0, 'id', 'pid');
        return $tree;
    }

    // 获取文章分类选择列表
    public function getSelect()
    {
        $result = parent::table('ay_content_cate')->field('pid,id,name')
            ->where("acode='" . session('acode') . "'")
            ->order('pid,sorting,id')
            ->select();
        $tree = get_tree($result, 0, 'id', 'pid');
        return $tree;
    }


    // 获取列表文章分类选择列表
    public function getListSelect()
    {
        $field = array('*,id as scode');

        $result = parent::table('ay_content_cate')->field($field)
            ->where("acode='" . session('acode') . "'")
            ->order('pid,sorting,id')
            ->select();
        $tree = get_tree($result, 0, 'id', 'pid');
        // 对于父栏目非列表的栏目进行追加到后面
        foreach ($result as $value) {
            if ($value->pid != 0 && result_value_search($value->pid, $result, 'id') === false) {
                $value->son = get_tree($result, $value->id, 'id', 'pid');
                $tree[] = $value;
            }
        }
        return $tree;
    }

    public function checkType($where)
    {
        return parent::table('ay_content_cate')->field('id')
            ->where($where)
            ->find();
    }

    // 获取详情
    public function getCate($id)
    {
        $field = array('*');

        return parent::table('ay_content_cate')->field($field)
            ->where("id='$id'")
            ->where("acode='" . session('acode') . "'")
            ->find();
    }


    // 添加文章分类
    public function addCate(array $data)
    {
        return parent::table('ay_content_cate')->autoTime()->insert($data);
    }

    // 删除文章分类及内容
    public function delCate($id)
    {
        $this->scodes = array(); // 先清空
        $scodes = $this->getSubScodes($id); // 获取全部子类
        return parent::table('ay_content_cate')->in('id', $scodes)
            ->where("acode='" . session('acode') . "'")
            ->delete();
    }

    // 批量删除栏目及内容
    public function delCateList($scodes)
    {
        $this->scodes = array(); // 先清空
        foreach ($scodes as $value) {
            $allscode = $this->getSubScodes($value); // 获取全部子类
        }
        return parent::table('ay_content_cate')->in('id', $allscode)
            ->where("acode='" . session('acode') . "'")
            ->delete();
    }

    // 修改文章分类资料
    public function modCate($id, $data)
    {
        $result = parent::table('ay_content_cate')->autoTime()
            ->where("id='$id'")
            ->where("acode='" . session('acode') . "'")
            ->update($data);
        return $result;
    }

    // 修改文章分类排序
    public function modCateSorting($id, $data)
    {
        $result = parent::table('ay_content_cate')->autoTime()
            ->where("id='$id'")
            ->where("acode='" . session('acode') . "'")
            ->update($data);
        return $result;
    }

    // 分类子类集
    private function getSubScodes($id)
    {
        if (! $id) {
            return;
        }
        $this->scodes[] = $id;
        $subs = parent::table('ay_content_cate')->where("pid='$id'")->column('id');
        if ($subs) {
            foreach ($subs as $value) {
                $this->getSubScodes($value);
            }
        }
        return $this->scodes;
    }

}